//
//  LoginViewController.m
//  MoXiProject
//
//  Created by 周筱雯 on 2017/6/29.
//  Copyright © 2017年 周筱雯. All rights reserved.
//

#import "LoginViewController.h"
#import "RegisterViewController.h"
#import "ForgetPwdViewController.h"
#import "LoginRegViewModel.h"


@interface LoginViewController ()

@property (weak, nonatomic) IBOutlet UIButton *btnRegister;
@property (weak, nonatomic) IBOutlet UIButton *btnForgerPwd;

@property (weak, nonatomic) IBOutlet UITextField *loginPhone;
@property (weak, nonatomic) IBOutlet UITextField *loginPwd;
@property (weak, nonatomic) IBOutlet UIButton *btnLoginClick;

@property (nonatomic,strong) LoginRegViewModel * loginViewModel;
@end

@implementation LoginViewController

-(LoginRegViewModel *) loginViewModel{
    if (!_loginViewModel) {
        _loginViewModel = [[LoginRegViewModel alloc] init];
    }
    return _loginViewModel;
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.loginPwd setText:@""];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self.navigationItem setTitle:@"登录"];
    [self.view setBackgroundColor:DEF_COLOR_F0F0F0];
    
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem backItemWithTarget:self action:@selector(backHome)];
    
    /**
     注册
     */
    [[self.btnRegister rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        
        RegisterViewController * registerVC = [[RegisterViewController alloc] init];
        [self.navigationController pushViewController:registerVC animated:YES];
        
    }];
    
    /**
     忘记密码
     */
    [[self.btnForgerPwd rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        
        ForgetPwdViewController * forgetVC = [[ForgetPwdViewController alloc] init];
        [self.navigationController pushViewController:forgetVC animated:YES];
        
    }];
    
    /**
     登录
     */
    [[self.btnLoginClick rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        
        if ([self ifelseLogin]) {
         
            [self loginNetWork];
        }
    }];
}

-(void) loginNetWork{
    
    [self.loginPhone resignFirstResponder];
    [self.loginPwd resignFirstResponder];
    
    [MXUtility showMBProgress:self.view message:LoadingText];
    
    [[self.loginViewModel LoginRequestParameter:self.loginPhone.text pwd:self.loginPwd.text] subscribeNext:^(id result) {
                
        if([result[@"errorcode"] integerValue] == 0){
            
            [MXUtility showTips:@"登录成功 ~"];
            [self dismissViewControllerAnimated:YES completion:nil];
        }
        
        [MXUtility hideMBProgress:self.view];
    }error:^(NSError *error) {
        
        [MXUtility hideMBProgress:self.view];
    }];
}

-(BOOL) ifelseLogin{

    if ([MXUtility NSNULLString:self.loginPhone.text]) {
        [MXUtility showTips:@"登录名不能为空"];
        return false;
    }
    if ([MXUtility NSNULLString:self.loginPwd.text]) {
        [MXUtility showTips:@"登录密码不能为空"];
        return  false;
    }
    return true;
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)events{
    
    [self.loginPhone resignFirstResponder];
    [self.loginPwd resignFirstResponder];
}
-(void) backHome{
    
    [self dismissViewControllerAnimated:YES completion:nil];
    [DEF_MyAppDelegate setNowTabBarIndex:0];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
